1. 题目描述(中等难度)

*[warning] 90. 子集 II

2. 解法一:回溯

class Solution {
    List<List<Integer>> resp = new ArrayList<>();
    List<Integer> ans = new ArrayList<>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
      Arrays.sort(nums); 
      if(null == nums || nums.length == 0){
          return resp;
      }
      boolean[] used = new boolean[nums.length];
      backTracking(nums,0,used);
      return resp;
    }

    public void backTracking(int[] nums,int startIndex,boolean[] used){
        resp.add(new ArrayList<>(ans));
        if(startIndex >= nums.length){
            return;
        }
        for(int i=startIndex;i<nums.length;i++){
            if(used[i] == true || (i>0 && nums[i] == nums[i-1] && used[i-1] == false)){
                continue;
            }
            used[i] = true;
            ans.add(nums[i]);
            backTracking(nums,i+1,used);
            ans.remove(ans.size()-1);
            used[i] = false;
        }

    }
}
© gaohueric all right reserved,powered by Gitbook文件修订时间: 2021-12-08 23:22:22

results matching ""

    No results matching ""